//
//  LBWEmoticonToolBar.swift
//  weibo
//
//  Created by Limonwei on 15/12/15.
//  Copyright © 2015年 heima. All rights reserved.
//

//底部工具条选择表情类型视图

import UIKit

//按钮点击的协议
protocol LBWEmoticonToolBarDelegate: NSObjectProtocol {
    //代理方法
    func didSelectedButtonWithType(type: LBWEmoticonType)
}

//定义一个枚举 记录表情类型
enum LBWEmoticonType: Int {
    case Recrnt = 1000
    case Default = 1001
    case Emoji = 1002
    case Lxh = 1003
}

class LBWEmoticonToolBar: UIStackView {

    //记录代理
    weak var delegate: LBWEmoticonToolBarDelegate?
    
    //当前选中的按钮
    var currentSelectedButton: UIButton?
    
    //当前的section
    var currentSection: Int? {
        didSet{
            guard let section = currentSection else {
                return
            }
            
             // 判断如果当前选中的 button 的 tag 与 传入的 section(加上基数) 一样,直接返回
            if currentSelectedButton?.tag == section + 1000 {
                return
            }
            //取到section对应的按钮
            let button = viewWithTag(section + 1000)! as! UIButton
            
            selectedButton(button)
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
        //子控件的排列方向
        axis = .Horizontal
        
        //子控件大小相等且占满父控件
        distribution = .FillEqually
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置视图
    private func setupUI(){
        //添加4个按钮
        addChildButton("最近", bgImg: "compose_emotion_table_left", type: .Recrnt)
        addChildButton("默认", bgImg: "compose_emotion_table_mid", type: .Default)
        addChildButton("Emoji", bgImg: "compose_emotion_table_mid", type: .Emoji)
        addChildButton("浪小花", bgImg: "compose_emotion_table_right", type: .Lxh)

        
    }
    
    //添加按钮的方法
    private func addChildButton(title: String, bgImg: String, type: LBWEmoticonType){
        
        //初始化按钮
        let button = UIButton()

        //添加按钮点击事件
        button.addTarget(self, action: "childButtonClick:", forControlEvents: .TouchUpInside)
        
        //设置tag.用来区分按钮的类型
        button.tag = type.rawValue
        //设置文字
        button.setTitle(title, forState: .Normal)
        
        //设置字体
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        
        //设置不同状态文字的文字颜色
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)

        
        //设置不同状态的背景图片
        button.setBackgroundImage(UIImage(named: "\(bgImg)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(bgImg)_selected"), forState: UIControlState.Selected)
        //添加到当前控件内部
        addArrangedSubview(button)
    }
    
    //按钮点击的监听方法
    func childButtonClick(button: UIButton){
        
        // 如果当前选中的按钮是与当前点击的按钮一样,不做任何操作
        if currentSelectedButton == button {
            return
        }
        
        selectedButton(button)
        
        //实现代理方法
        delegate?.didSelectedButtonWithType(LBWEmoticonType(rawValue: button.tag)!)
        

        
    }
    
    //选中当前点击的按钮 取消之前的按钮先选中状态
    private func selectedButton(button: UIButton){
        //把之前的按钮取消选中
        currentSelectedButton?.selected = false
        
        //选中当前的
        button.selected = true
    
        //记录当前按钮
        currentSelectedButton = button
    }
}
































